<template>
  <div class="m-login" ref="mLogin">
    <div class="warp">
      <div class="layout-1">
        <div class="logo">
          <div class="img">
            <img src="../../assets/logo.png" alt="">
          </div>
        </div>
        <el-form :model="form" ref="loginForm" :rules="rules">
          <div class="input-item">
            <el-form-item label="电子邮件" prop="loginName">
              <el-input v-model.trim="form.loginName" placeholder="请输入"></el-input>
            </el-form-item>
          </div>
          <div class="input-item">
            <el-form-item label="密码" prop="pwd">
              <el-input v-model.trim="form.pwd" placeholder="请输入" type="password"></el-input>
            </el-form-item>
          </div>
          <div class="input-item active">
            <el-button type="primary" @click="submit" :loading="loading">登陆</el-button>
            <el-button plain @click="reset">重置</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  import UserAPI from "@/Api/user";
  import * as Utils from '@/utils'
  import {mapActions} from 'vuex'
  export default {
    name: 'Login',
    data () {
      return {
        form: {
          loginName: '',
          pwd: ''
        },
        rules: {
          loginName: [
            {required: true, trigger: 'blur', message: '请输入登录邮箱'},
            {type: 'email', trigger: 'blur', message: '请输入登录邮箱'},
          ],
          pwd: [
            {required: true, trigger: 'blur', message: '请输入登录密码'}
          ]
        },
        loading: false,
      }
    },
    mounted(){
      let _self = this;
      this.$refs.mLogin.onkeyup = function (event) {
        var e = event || window.event;
        var keyCode = e.keyCode || e.which;
        switch (keyCode) {
          case 13:
            _self.submit();
            break;
        }
      }
    },
    methods: {
      submit() {
        this.$refs.loginForm.validate(flg => {
          if (!flg) {
            return;
          }
          if(this.loading) {
            return;
          }
          this.loading = true;
          this.login(this.form)
            .then(res => {
              this.loading = false;
              this.$message({
                message: '登陆成功',
                type: 'success'
              });
              this.$router.push({name: 'Index'});
             /* if(window.history.length <= 6) {
               this.$router.push({name: 'Index'});
              } else {
                this.$router.go(-1);
              }*/
            })
            .catch(error => {
              this.loading = false;
              if (error.code && error.code != '-999') {
                this.$notify.warning({
                  title: '温馨提示',
                  message: error.msg
                })
              }
            })
        })
      },
      reset() {
        this.$refs.loginForm.resetFields();
      },
      ...mapActions(
        ['login']
      )
    },
    created(){
      if(this.$store.state.app.userInfo) {
        window.location.reload()
      }
    }
  }
</script>

<style scoped type="text/css" lang="scss">
  @import "../../scss/variables";

  .m-login {
    height: 100%;
    min-height: 900px;
    background-color: $bg_A;;
    .warp {
      padding-top: 12%;
    }
    .layout-1 {
      width: 350px;
      box-shadow: 0 16px 28px 0 rgba(0, 0, 0, .22), 0 25px 55px 0 rgba(0, 0, 0, .21);
      border: solid 1px #dcdfe6;
      background-color: #fff;
      border-radius: 10px;
      padding: 20px 40px;
      margin: 0 auto;
      .logo {
        margin-top: 20px;
        text-align: center;
        margin-bottom: 15px;
        .img {
          display: inline-block;
          height: 80px;
          img {
            height: 100%;
          }
        }
      }
      .title {
        text-align: center;
        font-size: 22px;
        line-height: 30px;
      }
      .input-item {
        margin-bottom: 20px;
      }
      .active {
        text-align: center;
        display: flex;
        justify-content: space-between;
        margin-top: 20px;
        .el-button {
          width: 47%;
        }
      }
    }
  }
</style>
